package com.plter.minios.app
{
	import com.plter.controls.plter.PSprite;
	import com.plter.log.PLogger;
	
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	public class AppMainBase extends PSprite
	{
		internal static var appMain:IAppMain;
		protected var log:PLogger;
		
		public function AppMainBase()
		{
			log=new PLogger(this);
			
			AppMainBase.appMain=this as IAppMain;
			if(AppMainBase.appMain==null){
				throw new AppMainError("Class "+getQualifiedClassName(this)+" must implements interface com.plter.minios.app.IMainApp");
			}
			
			if(stage!=null){
				invokeInitAppMethod();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,this_addToStageHandler);
			}
		}
		
		private function this_addToStageHandler(event:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,this_addToStageHandler);
			
			invokeInitAppMethod();
		}
		
		private function invokeInitAppMethod():void{
			this["initApp"]();
		}
	}
}